//
//  UserInfoViewController.m
//  TeacherNet
//
//  Created by slz on 2017/12/19.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserChangeInfoViewController.h"

@interface UserInfoTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *leftLabel;
@property (nonatomic,strong) UILabel *rightLabel;
@end
@implementation UserInfoTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        _leftLabel = [UILabel new];
        _leftLabel.textColor = kDark_TextColor_Default;
        _leftLabel.fontSize = kFont_Size_Cotent_Nomal;
        [self.contentView addSubview:_leftLabel];
        [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(30);
            make.width.mas_equalTo(80);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
        }];
        
        _rightLabel = [UILabel new];
        _rightLabel.textColor = kLight_TextColor_Default;
        _rightLabel.fontSize = kFont_Size_Cotent_Nomal;
        [self.contentView addSubview:_rightLabel];
        [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_leftLabel.mas_right).offset(12);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
        }];
        
        UIView * lineView = [UIView quickLineColorView];
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self.contentView);
            make.height.mas_equalTo(.5);
        }];
    }
    return self;
}
@end

@interface UserInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UserChangeInfoDelegate>
@property (nonatomic,strong) NSArray *leftItems;
@property (nonatomic,strong) NSArray *rightItems;
@property (nonatomic,strong) UIImageView *userIamgeView;
@property (nonatomic,strong) YWTableView *tableView;
@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"个人信息"];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWIthTitle:@"修改" addTarget:self andAction:@selector(changgeAction)];

    
    MFWUserInfo * userInfo = [MFWSdkEnvironment sharedInstance].userInfo;
    
    _leftItems = @[@"用户名:   ",
                   @"手机号:   ",
                   @"姓  名:    ",
                   @"性  别:    ",
                   @"出生日期:",
                   @"个人签名:",
                   @"自我介绍:"];
    _rightItems = @[userInfo.nickName,userInfo.phone,userInfo.trueName,userInfo.sex,userInfo.birthday,userInfo.uIntro,userInfo.profile];
    
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    
    UIView *tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    [_tableView setTableHeaderView:tableHeaderView];
    
    
    UIView * lineView = [UIView quickLineColorView];
    [tableHeaderView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.mas_equalTo(tableHeaderView);
        make.height.mas_equalTo(.5);
    }];
    
    _userIamgeView = [[UIImageView alloc]init];
    [tableHeaderView addSubview:_userIamgeView];
    [_userIamgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.centerY.mas_equalTo(tableHeaderView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(64, 64));
    }];
    _userIamgeView.layer.cornerRadius = 32;
    _userIamgeView.layer.masksToBounds = YES;
    [_userIamgeView sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(userInfo.logoUrl)] randomPlaceHolder:YES];
    // Do any additional setup after loading the view.
}

- (void)updateUserInfo
{
    MFWUserInfo * userInfo = [MFWSdkEnvironment sharedInstance].userInfo;
    _rightItems = @[userInfo.nickName,userInfo.phone,userInfo.trueName,userInfo.sex,userInfo.birthday,userInfo.uIntro,userInfo.profile];
    [_userIamgeView sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(userInfo.logoUrl)] randomPlaceHolder:YES];
    [_tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _leftItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuserIdentifier = @"UserInfoTableViewCell";
    UserInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
    if (!cell){
        cell = [[UserInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.leftLabel.text = _leftItems[indexPath.row];
    cell.rightLabel.text = _rightItems[indexPath.row];
    return cell;
}

- (void)changgeAction{
    UserChangeInfoViewController * controller = [[UserChangeInfoViewController alloc]init];
    controller.delegate = self;
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)userDidChangeInfo
{
    [self updateUserInfo];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
